package com.sumer.psychology.comment.domain.model;

import com.sumer.psychology.common.Assert;
import com.sumer.psychology.common.domain.model.Author;

/**
 * 对评论的评论
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/10/9
 * Time: 9:55
 */
public class CommentToComment extends Comment {
    private Author object; // 回复谁

    public CommentToComment(CommentId identity, CommentHost host, String content, Author author, CommentType type, Author object) {
        super();
        initialize(identity, host, content, author, type);
        setObject(object);

        apply(new CommentToCommentCreatedEvent(
                commentId(),
                host(),
                content(),
                author(),
                commentType(),
                object()));
    }

    public CommentToComment(Comment comment, Author object) {
        this(
                comment.commentId(),
                comment.host(),
                comment.content(),
                comment.author(),
                comment.commentType(),
                object);
    }

    public CommentToComment() {
    }

    public Author object() {
        return object;
    }

    private void setObject(Author object) {
        Assert.notNull(object, "回复对象不能为空");

        this.object = object;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        CommentToComment comment = (CommentToComment) o;

        if (author() != null ? !author().equals(comment.author()) : comment.author() != null) return false;
        if (availability() != comment.availability()) return false;
        if (commentId() != null ? !commentId().equals(comment.commentId()) : comment.commentId() != null) return false;
        if (commentType() != comment.commentType()) return false;
        if (content() != null ? !content().equals(comment.content()) : comment.content() != null) return false;
        if (host() != null ? !host().equals(comment.host()) : comment.host() != null) return false;
        if (object != null ? !object.equals(comment.object()) : comment.object() != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = commentId() != null ? commentId().hashCode() : 0;
        result = 31 * result + (host() != null ? host().hashCode() : 0);
        result = 31 * result + (content() != null ? content().hashCode() : 0);
        result = 31 * result + (author() != null ? author().hashCode() : 0);
        result = 31 * result + (commentType() != null ? commentType().hashCode() : 0);
        result = 31 * result + (availability() != null ? availability().hashCode() : 0);
        result = 31 * result + (object() != null ? object().hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Comment{" +
                "commentId=" + commentId() +
                ", host=" + host() +
                ", content='" + content() + '\'' +
                ", author=" + author() +
                ", commentType=" + commentType() +
                ", createTime=" + createTime() +
                ", lastModifyTime=" + lastModifyTime() +
                ", availability=" + availability() +
                ", object=" + object() +
                '}';
    }
}
